package com.wl.mr.comment.step3;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CommentMapper extends Mapper<Text, BytesWritable,CommentBean, NullWritable> {
    @Override
    protected void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException {
        String[] lines = value.toString().split("\n");

        for (String line : lines) {
            CommentBean commentBean = parseStrToCommentBean(line);
            if(commentBean != null){
                context.write(commentBean,NullWritable.get());
            }
        }

    }

    public CommentBean parseStrToCommentBean(String line){
        if (StringUtils.isNotEmpty(line)){
            String[] fields = line.split("\t");

            if(fields.length >= 9){
                return new CommentBean(fields[0],fields[1],fields[2],Integer.parseInt(fields[3]), fields[4],fields[5],
                        fields[6],Integer.parseInt(fields[7]),fields[8]);
            }else{
                return null;
            }
        }else {
            return null;
        }
    }


}
